//
// File:	GuiMenuCreateGame.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Created:	29/04/2010 at 20:24:28
// Purpose:	
//

#include <QtCore/QDir>
#include <QtCore/QDirIterator>
#include "Base.hpp"
#include "Config.hpp"
#include "GuiMenuCreateGame.hpp"
#include "Parser.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
GuiMenuCreateGame::GuiMenuCreateGame(QWidget *pParent /* = NULL */)
	:	GuiPanel(pParent)
{
	//Menu
	setObjectName("MenuCreateGame");

	//Title
	GuiLabel *pTitle = new GuiLabel(this);
	pTitle->move(100, 85);
	pTitle->resize(500, 55);
	pTitle->setText("OPTIONS");
	pTitle->setObjectName("Title");

	//Back
	_menuButtons[ButtonBack] = new GuiPushButton(this);
	_menuButtons[ButtonBack]->setText("Back");
	_menuButtons[ButtonBack]->setObjectName("ButtonBack");
	_menuButtons[ButtonBack]->resize(80, 25);
	_menuButtons[ButtonBack]->move(670, 550);

	//Create Game
	_menuButtons[ButtonAccept] = new GuiPushButton(this);
	_menuButtons[ButtonAccept]->setText("Accept");
	_menuButtons[ButtonAccept]->setObjectName("ButtonAccept");
	_menuButtons[ButtonAccept]->resize(80, 25);
	_menuButtons[ButtonAccept]->move(570, 550);

	//Group Box
	GuiGroupBox *pGroupBox = new GuiGroupBox(this);
	pGroupBox->move(100, 150);
	pGroupBox->resize(650, 390);

	//Game Rule
	_menuLabels[LabelGameRule] = new GuiLabel(pGroupBox);
	_menuLabels[LabelGameRule]->move(50, 50);
	_menuLabels[LabelGameRule]->resize(200, 20);
	_menuLabels[LabelGameRule]->setText("Game Mode:");
	_menuComboBoxes[ComboBoxGameRule] = new GuiComboBox(pGroupBox);
	_menuComboBoxes[ComboBoxGameRule]->move(250, 50);
	_menuComboBoxes[ComboBoxGameRule]->resize(300, 20);

	//Level
	_menuLabels[LabelLevel] = new GuiLabel(pGroupBox);
	_menuLabels[LabelLevel]->move(50, 100);
	_menuLabels[LabelLevel]->resize(200, 20);
	_menuLabels[LabelLevel]->setText("Level:");
	_menuComboBoxes[ComboBoxLevel] = new GuiComboBox(pGroupBox);
	_menuComboBoxes[ComboBoxLevel]->move(250, 100);
	_menuComboBoxes[ComboBoxLevel]->resize(300, 20);

	//Current Level Image
	_menuImages[ImageCurrentLevel] = new GuiImage(pGroupBox);
	_menuImages[ImageCurrentLevel]->move(250, 150);
	_menuImages[ImageCurrentLevel]->resize(300, 200);

	//Slots
	QObject::connect(_menuButtons[ButtonBack], SIGNAL(clicked()), this, SLOT(onBackClicked()));
	QObject::connect(_menuButtons[ButtonAccept], SIGNAL(clicked()), this, SLOT(onAcceptClicked()));
	QObject::connect(_menuComboBoxes[ComboBoxGameRule], SIGNAL(currentIndexChanged(const QString &)), this, SLOT(onGameRuleCurrentIndexChanged(const QString &)));
	QObject::connect(_menuComboBoxes[ComboBoxLevel], SIGNAL(currentIndexChanged(const QString &)), this, SLOT(onLevelCurrentIndexChanged(const QString &)));
}

GuiMenuCreateGame::~GuiMenuCreateGame()
{
}

//------------------------------------------------------------------
// Events
//------------------------------------------------------------------
void GuiMenuCreateGame::showEvent(QShowEvent *e)
{
	//Clear Levels List
	_menuComboBoxes[ComboBoxLevel]->clear();

	//Destroy Current Level Image
	_menuImages[ImageCurrentLevel]->setPixmap(QPixmap());

	//Clear Game Rules List
	_menuComboBoxes[ComboBoxGameRule]->clear();

	//Update Game Rules List
	QDir rulesDirectory("data/levels/");
	if (rulesDirectory.isReadable())
	{
		QFileInfoList rulesList = rulesDirectory.entryInfoList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDir::Name);
		QFileInfoList::iterator it = rulesList.begin();
		QFileInfoList::iterator ite = rulesList.end();
		for (; it != ite; ++it)
		{
			_menuComboBoxes[ComboBoxGameRule]->addItem(it->fileName());
		}
	}
	
	//Parent's Show Event
	GuiPanel::showEvent(e);
}

//------------------------------------------------------------------
// Slots
//------------------------------------------------------------------
void GuiMenuCreateGame::onBackClicked()
{
	//Show Main Menu
	GlobalVariables::GuiManager::Get().ShowPanel("menumain");
}

void GuiMenuCreateGame::onAcceptClicked()
{
	//HACK: go back to Main Menu
	onBackClicked();

	//TODO: do not do anything if no levels is selected or no gamerule is available.
	const std::string gamerule = _menuComboBoxes[ComboBoxGameRule]->currentText().toStdString();
	const std::string level = _menuComboBoxes[ComboBoxLevel]->currentText().toStdString();
	GlobalVariables::Engine::Get().StartGame(gamerule, level);
}

void GuiMenuCreateGame::onGameRuleCurrentIndexChanged(const QString &name)
{
	//Clear Levels List
	_menuComboBoxes[ComboBoxLevel]->clear();

	//Update Levels List
	QDir mapsDirectory("data/levels/" + name);
	if (mapsDirectory.isReadable())
	{
		QStringList namesFilters;
		namesFilters << "*.lvl";

		QFileInfoList mapsList = mapsDirectory.entryInfoList(namesFilters, QDir::Files | QDir::NoSymLinks, QDir::Name);
		QFileInfoList::iterator it = mapsList.begin();
		QFileInfoList::iterator ite = mapsList.end();
		for (; it != ite; ++it)
		{
			_menuComboBoxes[ComboBoxLevel]->addItem(it->fileName());
		}
	}
}

void GuiMenuCreateGame::onLevelCurrentIndexChanged(const QString &name)
{
	//Level Path
	const QString levelpath = "data/levels/" + _menuComboBoxes[ComboBoxGameRule]->currentText() + "/" + name;

	//Destroy Current Level Image
	_menuImages[ImageCurrentLevel]->setPixmap(QPixmap());

	//Update Current Level Image
	Parser parser;
	if (parser.ParseFromFile(levelpath.toStdString()))
	{
		Parser::Element *pLevelElement = parser.GetRootElement().FindChild("level");
		if (pLevelElement != NULL)
		{
			const QString levelshotPath = QString::fromStdString(pLevelElement->GetValueForKey("levelshot"));
			if (!levelshotPath.isEmpty())
			{
				QPixmap pixmap = QPixmap::fromImage(QImage(levelshotPath));
				float flScaleW = static_cast<float>(_menuImages[ImageCurrentLevel]->width() / static_cast<float>(pixmap.width()));
				float flScaleH = static_cast<float>(_menuImages[ImageCurrentLevel]->height() / static_cast<float>(pixmap.height()));
				if (flScaleW != 1.0f && flScaleH != 1.0f)
					pixmap = pixmap.scaled(flScaleW * pixmap.width(), flScaleH * pixmap.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
				_menuImages[ImageCurrentLevel]->setPixmap(pixmap);
			}
		}
	}
}
